<%-- 
    Document   : ViewCart
    Created on : Nov 22, 2012, 9:31:14 AM
    Author     : tuan
--%>

<%@page import="data.Order"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Iterator"%>
<%@page import="data.ProductDAL"%>
<%@page import="data.ProductEntity"%>
<%@page import="java.util.Collection"%>
<%@page import="java.util.Enumeration"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <div class="page secondary">
            <div class="page-header">
                <div class="page-header-content">
                    <h1>Shopping Cart Detail</h1>
                </div>
            </div>
            <hr color="blue">
            <div class="page-region">
                <div class="page-region-content">
                    <div class="span8">
                        <%
                            float total = 0;
                            try {
                                Collection<Order> orders = (Collection<Order>) session.getAttribute("orders");
                                if (!orders.isEmpty()) {
            %>
                        <table class="hovered"> 
                            <tr>
                                <td>Product ID</td>
                                <td>Name</td>
                                <td>Price(USD)</td>
                                <td>Amount</td>
                                <td>Value(USD)</td>
                            </tr>                           

                            <%
                                Iterator<Order> iter = orders.iterator();

                                while (iter.hasNext()) {
                                    Order entity = iter.next();
                                    
                                    ProductEntity proEntity = ProductDAL.searchById(entity.getProductId());
                                    total += proEntity.getUnitPrice() * entity.getQuantity();
                            %>
                            <tr>
                                <td><%= proEntity.getProductId()%></td>
                                <td><%= proEntity.getProductName()%></td>
                                <td><%= proEntity.getUnitPrice()%></td>
                                <td><%= entity.getQuantity()%></td>
                                <td><%= proEntity.getUnitPrice() * entity.getQuantity()%></td>
                            </tr>


                            <% }
                            %>
                            <tr>
                                <td colspan="4" align="right">Total:</td>
                                <td><%= total%></td>
                            </tr>
                        </table>
                        <%} else {
                        %>
                        <h3 style="color: red">Cart is empty!</h3>
                        <%                            }
                        } catch (Exception e) {%>
                        <h3><%= "Error: " + e.getMessage()%></h3>
                        <% }
                        %>

                    </div>
                    <a class="button" href="newCart.jsp">Check out!</a>
                    <br>
                    <a class="button" href="viewProduct.jsp">Home Page</a>
                </div>
            </div>
        </div>
    </body>
</html>
